package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;



/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-12 20:22
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addsetmeal(SetmealDTO setmealDTO) {
        //将setmealDTO的数据复制给实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //添加套餐 通过主键返回 拿到id封装到套餐的实体类里面
         setmealMapper.addsetmeal(setmeal);
         //得到套餐与菜品关连的id
        Long id = setmeal.getId();
        //拿到前端传递过来的套餐菜品关系的集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历套餐
        setmealDishes.forEach(setmealDish ->
                //将拿到id分装到菜品套餐表的实体类中
                setmealDish.setSetmealId(id)
                );
        setmealDishMapper.addsetmealDish(setmealDishes);
    }

    /**
     * 套餐分页查询相关接口
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSetmea(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVO=setmealMapper.pageSetmea(setmealPageQueryDTO);
        return new PageResult(setmealVO.getTotal(),setmealVO.getResult());
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //创建实体类
        Setmeal setmeal = new Setmeal();
        //拷贝
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改
        setmealMapper.update(setmeal);
        //删除套餐菜品表的数据
        setmealDishMapper.deleteId(setmealDTO.getId());
        //拿到一个套餐对应的多个菜品的集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历将菜品套餐表的实例对象取出
        if (setmealDishes!=null &&setmealDishes.size()>0  ) {
            setmealDishes.forEach(setmealDish ->
                    //将套餐表的Id存入菜品套餐表的实体对象  将值给Id赋给菜品套餐表与套餐对应的外键id :SetmealId
                    setmealDish.setSetmealId(setmealDTO.getId()));
        }
                setmealDishMapper.addsetmealDish(setmealDishes);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO SetmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,SetmealVO);
        /*List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
        SetmealVO.setSetmealDishes(setmealDishes);*/
        return SetmealVO;
    }

    /**
     * 起售停售接口
     * @param status
     * @param id
     */
    @Override
    public void updatestatus(Integer status, Long id) {
        Setmeal build = Setmeal.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();
        setmealMapper.update(build);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deletes(List<Long> ids) {
        //先根据ids查出套餐 套餐表在起售状态的有几个
         Integer contn= setmealMapper.getbyids(ids);
        //有报错
        if (contn>0){
            throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
        }
        //查出菜品套餐表关联的的有几个
        contn= setmealDishMapper.getbyids(ids);
        //有报错
        if (contn>0){
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表
        setmealDishMapper.deleteids(ids);
        //删除套餐表
        setmealMapper.deleteids(ids);
        //有报错
        //两种状态都没有
        //删除套餐菜品表,套餐表
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
